package com.mypaper.list;

import com.mypaper.elem.DataElement;
import com.mypaper.elem.LinkedListNode;


public abstract class LinkedListClass {
	
	public LinkedListNode first;
	public LinkedListNode last;
	public int count;
	
	public boolean isEmpty(){
		return (first==null);
	}
	public LinkedListClass(){
		//构造函数
		first=null;
		last=null;
		count=0;
	}	
	public void initializeList(){
		first=null;
		last=null;
		count=0;
	}
	public void print(){
		LinkedListNode current;
		current=first;
		if(current == null)System.out.println("null");
		while(current!=null){
			System.out.println(current.info);
			current=current.next;
		}
	}
	public int length(){
		return count;
	}
	public DataElement front(){
		//返回第一个结点的内容
		DataElement temp=first.info.getCopy();
		return temp;
	}
	public DataElement back(){
		//返回最后一个结点的内容
		DataElement temp=last.info.getCopy();
		return temp;
	}
	private void copy(LinkedListClass otherList){
		//复制一个链表的内容
		LinkedListNode newNode;
		LinkedListNode current;
		first=null;
		if(otherList==null){
			first=null;
			last=null;
			count=0;
		}else{
			count=otherList.count;
			current=otherList.first;
			first=new LinkedListNode();
			first.info=current.info.getCopy();
			first.next=null;
			last=first;
			current=current.next;
			
			while(current!=null){	
				newNode=new LinkedListNode();
				newNode.info=current.info.getCopy();
				newNode.next=null;
				
				last.next=newNode;
				last=newNode;
				current=current.next;
			}
			newNode=null;
		}
	}
	public LinkedListClass(LinkedListClass otherList){
		//构造函数
		copy(otherList);
	}
	public void copyList(LinkedListClass otherList){
		if(this!=otherList){
			copy(otherList);
		}
	}
	public abstract DataElement search(DataElement searchItem);//搜索结点
	public abstract void deleteNode(DataElement deleteItem);//删除结点
}
